
package com.zhaogang.annotation;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SystemLog;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.service.SystemLogService;

@Component
@Aspect
public class SystemLogAspect {

	@Autowired
	private SystemLogService systemLogService;

	private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

	@Pointcut("execution(* com.zhaogang.service..*.*(..)) "
			+ "&& !execution(* com.zhaogang.service.impl.SystemLogServiceImpl.*(..)) "
			+ "&& !execution(* com.zhaogang.service.impl.SaleAccountServiceImpl.*(..))"
			+ "&& !execution(* com.zhaogang.service.impl.OrderServiceImpl.*(..))"
			)
	public void controllerAspect() {
	}

	@Before("controllerAspect()")
	public void doBefore(JoinPoint joinPoint) {
		System.out.println("==========执行前置通知===============");

	}

	@Before("controllerAspect()")
	public void around(JoinPoint joinPoint) {
		System.out.println("==========执行环绕===============");
	}

	@After("controllerAspect()")
	public void after(JoinPoint joinPoint) {
		System.out.println("==========执行后置通知===============");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		HttpSession session = request.getSession();
		SystemLog log = new SystemLog();
		try {
			int sexs =(Integer) session.getAttribute("sex");
		if(sexs == 0){
			SaleAccount userType1 = (SaleAccount) session.getAttribute("saleAccount");
			   if(userType1!=null){
					 String userType = userType1.getSaleAccount();
					 log.setUserName(userType);
						 }
				 else{
					 
					 String userType="aaa";
					 log.setUserName(userType);
					 }
			
		}else if(sexs==1){
			UserAccount userType1 = (UserAccount) session.getAttribute("userToken");
			   if(userType1!=null){
					 String userType = userType1.getUserAccount();
					 log.setUserName(userType);
						 }
				 else{
					 String userType="aaa";
					 log.setUserName(userType);
					 }
		}
		} catch (Exception e) {
			log.setUserName("bbb");
			System.out.println("sexs为空");
		}
		// String userType= ((SaleAccount) userType1).getSaleAccount();
		//String userType = "xiaoqiang";
		// String userType= userType1.getSaleAccount();/*(String)session.getAttribute(UserToken);*/
		//String ip = request.getRemoteAddr();// 请求ip
		String ip = request.getHeader(" x-forwarded-for ");  
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {  
            ip = request.getHeader(" Proxy-Client-IP ");  
        }  
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {  
            ip = request.getHeader(" WL-Proxy-Client-IP ");  
        }  
        if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr(); 
        }
	    String path = request.getServletPath();
	    Date date=new Date(); 	    
	    log.setIp(ip);
	    log.setPath(path);
	    log.setDate(date);
	   /* System.out.println("请求人:" + userType);
		System.out.println("请求IP:" + ip);
		System.out.println("请求url:"+path);
		System.out.println("当前时间:"+date);*/
		//插入数据库
		systemLogService.insertInto(log);
		System.out.println("=====controller后置通知结束=====");
	}

	@AfterReturning("controllerAspect()")
	public void afterReturn(JoinPoint joinPoint) {
		
		if (logger.isInfoEnabled()) {
			logger.info("afterReturn " + joinPoint);
		}
	}

	@AfterThrowing(pointcut = "controllerAspect()", throwing = "e")
	public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
	}
}
